﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;
    public class Activity_Gifts
    {
        public int Add(Chain.Model.Activity_Gifts model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into Activity_Gifts(");
            builder.Append("ActivityID,ScoreID,MoneyID,ShareID,ExtendID,PresentID,TicketID,RebateID,isdel,MoneyRebateID)");
            builder.Append(" values (");
            builder.Append("@ActivityID,@ScoreID,@MoneyID,@ShareID,@ExtendID,@PresentID,@TicketID,@RebateID,0,@MoneyRebateID)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] {  
                new SqlParameter("@ActivityID", SqlDbType.Int,4),
                new SqlParameter("@ScoreID", SqlDbType.Int,4),
                 new SqlParameter("@MoneyID", SqlDbType.Int,4),
                new SqlParameter("@ShareID", SqlDbType.Int,4),
                 new SqlParameter("@ExtendID", SqlDbType.Int,4),
                new SqlParameter("@PresentID", SqlDbType.Int,4),
                 new SqlParameter("@TicketID", SqlDbType.Int,4),
                new SqlParameter("@RebateID", SqlDbType.Int,4),
                new SqlParameter("@MoneyRebateID", SqlDbType.Int,4)


             };
            cmdParms[0].Value = model.ActivityID;
            cmdParms[1].Value = model.ScoreID;
            cmdParms[2].Value = model.MoneyID;
            cmdParms[3].Value = model.ShareID;
            cmdParms[4].Value = model.ExtendID;
            cmdParms[5].Value = model.PresentID;
            cmdParms[6].Value = model.TicketID;
            cmdParms[7].Value = model.RebateID;
            cmdParms[8].Value = model.MoneyRebateID;

            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }



        public bool Exists(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from Activity_Gifts");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }


        public DataTable GetAwardsInfo(int ActivityID)
        {
            StringBuilder builder2 = new StringBuilder();
            builder2.Append(" select * into #t1 from Activity_Gifts where ActivityID=@ActivityID ");
            builder2.Append(" select  CONVERT(varchar(10),Nums) as value,'积分' as type,ID as id from ScoreMoneyAwards where ID=(select ScoreID from #t1) ");
            builder2.Append(" union all ");
            builder2.Append(" select  CONVERT(varchar(10),Nums) as value,'储值' as type,ID as id  from ScoreMoneyAwards where ID=(select MoneyID from #t1) ");
            builder2.Append(" union all ");
            builder2.Append(" select CONVERT(varchar(10),Level1)+'%,'+CONVERT(varchar(10),Level2)+'%,'+CONVERT(varchar(10),Level3)+'%' as value,'消费返利' as type,ID as id  from RebateAwards where ID=(select RebateID from #t1) ");
            builder2.Append(" union all ");
            builder2.Append(" select CONVERT(varchar(10),Level1)+'%,'+CONVERT(varchar(10),Level2)+'%,'+CONVERT(varchar(10),Level3)+'%' as value,'储值返利' as type,ID as id  from RebateAwards where ID=(select MoneyRebateID from #t1) ");
            builder2.Append(" union all ");
            builder2.Append(" select CouponTitle as value,'代金券' as type,ID as id from Coupon where ID=(select TicketID from #t1) ");
            builder2.Append(" union all ");
            builder2.Append(" select PresentsName as value,'小礼品' as type,ID as id  from PresentAwards where ID=(select PresentID from #t1) ");
            builder2.Append(" union all ");
            builder2.Append(" select CONVERT(varchar(10),Score)+'积分,'+CONVERT(varchar(10),Money)+'储值,'+CONVERT(varchar(10),WxPocket)+'红包' as value,'分享奖励' as type,ID as id  from Share_ExtendAwards where ID=(select ShareID from #t1) ");
            builder2.Append(" union all ");
            builder2.Append(" select CONVERT(varchar(10),Score)+'积分,'+CONVERT(varchar(10),Money)+'储值,'+CONVERT(varchar(10),WxPocket)+'红包' as value,'推广奖励' as type,ID as id  from Share_ExtendAwards where ID=(select ExtendID from #t1) ");
            builder2.Append(" drop table #t1 ");

            SqlParameter[] cmdParms2 = new SqlParameter[] { new SqlParameter("@ActivityID", SqlDbType.Int, 4) };
            cmdParms2[0].Value = ActivityID;

            DataTable dt = DbHelperSQL.Query(builder2.ToString(), cmdParms2).Tables[0];
            return dt;

        }


        public DataTable GetItem(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from Activity_Gifts ");
            builder.Append(" where ActivityID=@ID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            return DbHelperSQL.Query(builder.ToString(), cmdParms).Tables[0];
        }

    }


}
